
package cn.nubia.admin.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by justin on 15/6/29.
 */
public class DateUtil {

    public static final int SECONDS_OF_YEAR = 60*60*24*365;
    /**
     * 返回当前时间（s）
     *
     * @return
     */
    public static Integer getCurrentTime() {
        return Integer.valueOf(String.valueOf(System.currentTimeMillis() / 1000));
    }


    /**
     * 返回范围的开始时间
     *
     * @param range 时间范围
     * @return 开始时间。默认以00:00:00做结尾
     */
    public static Date getBeginDateFromRange(String range) {
        if (range == null || range == "") {
            return null;
        }
        String[] rangeStr = range.split("-");
        String time = rangeStr[0] + " 00:00:00";//时间格式的字符串
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date startDate = null;
        try {
            startDate = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return startDate;
    }


    /**
     * 返回范围的结束时间
     *
     * @param range 时间范围
     * @return 结束时间，默认以23:59:59做结尾
     */
    public static Date getEndDateFromRage(String range) {
        if (range == null) {
            return null;
        }
        String[] rangeStr = range.split("-");
        String time = rangeStr[1] + " 23:59:59";//时间格式的字符串
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date endDate = null;
        try {
            endDate = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDate;
    }

    public static Integer getIntegerTime(Date date){
        if(date == null)return 0;
        if(date.getTime()/1000>=Integer.MAX_VALUE)return Integer.MAX_VALUE;
        return Integer.valueOf(String.valueOf(date.getTime()/1000).trim());
    }

    public static Date getDateFromInt(Integer time){
        return new Date(Long.valueOf(String.valueOf(time)+"000"));
    }

    /**
     * 时间格式为yyyy年MM月dd日
     * @param time
     * @return
     */
    public static String getFormatDate(Integer time){
        if(time==null||time<=0)return "";
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd");
        return myFmt.format(new Date(Long.parseLong(time+"000")));
    }



}
